﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;

using TestUploadDownload.FileTransferServiceRef;

namespace TestUploadDownload
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();            
        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {   
        }

        private void btnUpload_Click(object sender, RoutedEventArgs e)
        {
            Cursor = Cursors.Wait;
            try
            {
                // get some info about the input file
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(textBox1.Text);

                // show start message
                Console.WriteLine("Starting uploading " + fileInfo.Name);
                Console.WriteLine("Size : " + fileInfo.Length);

                // open input stream
                using (System.IO.FileStream stream = new System.IO.FileStream(textBox1.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read))
                {
                    using (StreamWithProgress uploadStreamWithProgress = new StreamWithProgress(stream))
                    {
                        uploadStreamWithProgress.ProgressChanged += uploadStreamWithProgress_ProgressChanged;

                        // start service client
                        FileTransferServiceClient client = new FileTransferServiceClient("BasicHttpBinding_FileTransferService");

                        UploadFileInfo info = new UploadFileInfo(DocumentType.SermonAudio, fileInfo.Name, 24, uploadStreamWithProgress);
                        // upload file
                        client.UploadFile(info);

                        //LogText("Done!");

                        // close service client
                        client.Close();                        
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception : " + ex.Message);
                //if (ex.InnerException != null) LogText("Inner Exception : " + ex.InnerException.Message);
            }
            finally
            {
                Cursor = Cursors.Arrow;
            }
        }

        void uploadStreamWithProgress_ProgressChanged(object sender, StreamWithProgress.ProgressChangedEventArgs e)
        {
            if (e.Length != 0)
            {
                progressBar1.Value = (int)(e.BytesRead * 100 / e.Length);                
            }
        }
    }
}
